package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentPersonGroup;
import com.mxpio.erp.common.equipment.entity.EquipmentPersonGroupDetail;
import com.mxpio.erp.common.equipment.service.EquipmentPersonGroupDetailService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 人员群组管理明细Service业务层处理
 *
 * @author Cheng
 * @date 2023-06-28
 */
@Service
public class EquipmentPersonGroupDetailServiceImpl extends BaseServiceImpl<EquipmentPersonGroupDetail> implements EquipmentPersonGroupDetailService {

    @Override
    @Transactional(readOnly = true)
    public List<String> selectByUsername(String username) {
        List<EquipmentPersonGroupDetail> list = JpaUtil.linq(EquipmentPersonGroupDetail.class).equal("userId", username).list();
        List<String> collect = list.stream().map(item -> item.getGroupId()).collect(Collectors.toList());
        return collect;
    }

    @Override
    @Transactional(readOnly = true)
    public List<String> selectUserIdByGroupId(String groupId) {
        List<EquipmentPersonGroupDetail> list = JpaUtil.linq(EquipmentPersonGroupDetail.class).equal("groupId", groupId).list();
        return list.stream().map(EquipmentPersonGroupDetail::getUserId).collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public List<EquipmentPersonGroupDetail> listByCode(String groupCode) {
        return JpaUtil.linq(EquipmentPersonGroupDetail.class)
                .exists(EquipmentPersonGroup.class)
                    .equalProperty("id","groupId")
                    .equal("groupCode",groupCode)
                .end()
                .list();
    }
}
